program principal
interface

function trapecio(a,b)result(res3)
	real(kind=8)::a,b,resul
end function

function simpson(a,b)result(res4)
	real(kind=8)::a,b,resul
end function

subroutine datos(a,b,maxit,tol)
	real(kind=8)::tol,a,b
	integer::maxit
end subroutine

function funcion(x)result(res1)
	real(Kind=8),intent(in)::x
	real(kind=8)::res1
end function


subroutine dicotomia(a,b,it,tol)
	real(kind=8)::tol,a,b,x,ant
	integer::it,i
end subroutine

end interface

real(kind=8)::error,a,b,z,c		
	integer::it
	call datos(a,b,it,error)
	z=trapecio(a,b)
	c=simpson(a,b)
print*,z,'  ',c
	!call dicotomia(a,b,it,error)
end program

 recursive subroutine datos(a,b,maxit,tol)
	real(kind=8)::tol,a,b
	integer::maxit
	print*,'¿cual es el intervalo de partida?'
	read(5,*),a
	read(5,*),b
	if(funcion(a)==0._8) then 
		print*,'la raiz es: ',a
		stop('')
	else	if (funcion(b)==0._8) then 
		print*,'la raiz es: ',b
		stop
	end if
	if((funcion(a)*funcion(b))>0._8) then
		print*,'el intervalo no es valido,introduzca otro'
		call datos(a,b,maxit,tol)
	else
		print*,'¿numero maximo de iteraciones?'
		read(5,*),maxit
		print*,'¿tolerancia de error?'
		read(5,*)tol
	end if
	
end subroutine

function trapecio(a,b)result(res3)
	real(kind=8)::a,b,resul
	interface
		function funcion(x)result(res1)
			real(Kind=8),intent(in)::x
			real(kind=8)::res1
		end function
	end interface

	res3=(b-a)*0.5_8*(funcion(a)+funcion(b))
end function

function simpson(a,b)result(res4)
	real(kind=8)::a,b,resul
	interface
		function funcion(x)result(res1)
			real(Kind=8),intent(in)::x
			real(kind=8)::res1
		end function
	end interface
	
	res4=(b-a)/6._8*(funcion(a)+4._8*funcion((a+b)*0.5_8)+funcion(b))
end function

function funcion(x)result(res1)
	real(Kind=8),intent(in)::x
	real(kind=8)::res1
	res1=x**2._8-x - 1._8
end function

subroutine dicotomia(a,b,it,tol)
	real(kind=8)::tol,a,b,x,ant
	integer::it,i
	interface
		function funcion(x)result(res1)
			real(Kind=8),intent(in)::x
			real(kind=8)::res1
		end function
	end interface
	open(unit=1, file='dicotomia.dat',status='unknown')
	x=a
	do i=1,it,1
		ant=x
		x=(a+b)*0.5_8
		
		if(funcion(a)*funcion(x)<0) then
			b=x
		else
			a=x
		end if
		error=abs((x-ant)/x)

		write(1,*)'iteracion: ',i
		write(1,*)'Aproximacion'
		write(1,'(f17.14)'),x
		write(1,*)'error relativo: '
		write(1,'(e21.14)')error
		write(1,*)
		if(funcion(x)==0._8 .or. error<tol) then
			exit
		end if
	end do
	close(1)
end subroutine
















